package com.cxs.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.cxs.config.MqttGateway;
import com.cxs.domain.MsgModel;
import com.cxs.service.MqttService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author CXS
 * @Date 2024/1/26 17:47
 **/
@RestController
public class TestController {


    @Autowired
    private MqttService mqttService;

    @Autowired
    private MqttGateway mqttGateway;

    @PostMapping("addTopic")
    public String addTopic(String topic, Integer qos) {
        mqttService.addTopic(topic, qos);
        return "addTopic ok";
    }


    @DeleteMapping("removeTopic")
    public String removeTopic(String topic) {
        mqttService.removeTopic(topic);
        return "removeTopic ok";
    }


    @PostMapping("sendMsg")
    public String sendMsg(String topic, String msg) {
        MsgModel msgModel = new MsgModel(IdUtil.fastSimpleUUID(), msg, System.currentTimeMillis());
        mqttGateway.sendToMqtt(topic, JSONUtil.toJsonStr(msgModel));
        return "sendMsg ok";
    }


}
